package cs633.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

import cs633.util.Album;
import cs633.util.DBManager;
import cs633.util.Photo;

public class UserAlbumsAction extends ActionSupport implements ServletRequestAware {
	private static final long serialVersionUID = 7524102511514266657L;
	private HttpServletRequest request;
	private List<Album> albumList;
	private List<Photo> photoList;
	
	public String userPhotos() {
		Photo photo = null;
		String userId = request.getParameter("userId");
		String albumName = request.getParameter("albumName");
		
		Connection con = null;	
		List<Photo> aList = new ArrayList<Photo>();
		
		try {
			con = DBManager.getInstance().getConn();
			String sql = "SELECT A.ALBUM_LOC, B.IMG_NAME, B.IMG_TITLE, B.IMG_DESC FROM cs633.user_album A, cs633.user_img B, cs633.user C " +
					"WHERE A.USER_ALBUM_ID = B.USER_ALBUM_ID AND C.USER_SEQ_ID = A.USER_SEQ_ID AND C.USER_ID = ? AND A.ALBUM_NAME = ?";			
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, userId);
			pstmt.setString(2, albumName);
			
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				photo = new Photo();
				photo.setAlbumLoc(rs.getString("ALBUM_LOC"));
				photo.setPhotoName(rs.getString("IMG_NAME"));
				photo.setPhotoTitle(rs.getString("IMG_TITLE"));
				photo.setPhotoDesc(rs.getString("IMG_DESC"));
				
				aList.add(photo);
			}
			setPhotoList(aList);
			
		} catch (Exception e) {
			System.out.println("Error: " + e.getMessage());
		} finally {
			try {
				con.close();
			} catch (Exception e) {
				
			}
		}
		return SUCCESS;
	}
	
	public String userAlbums() {
		System.out.println("Called userAlbums!");
		Album album = null;
		String userId = request.getParameter("userId");
		Connection con = null;
		List<Album> aList = new ArrayList<Album>();
		try {
			con = DBManager.getInstance().getConn();
			String sql = "SELECT B.ALBUM_NAME FROM cs633.user A, cs633.user_album B WHERE A.USER_ID = ? AND A.USER_SEQ_ID = B.USER_SEQ_ID ORDER BY B.ALBUM_NAME";
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, userId);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				album = new Album();
				album.setAlbumName(rs.getString("ALBUM_NAME"));
				aList.add(album);
			}
			setAlbumList(aList);
			
		} catch (Exception e) {
			System.out.println("Error: " + e.getMessage());
		} finally {
			try {
				con.close();
			} catch (Exception e) {
				
			}
		}
		return SUCCESS;
	}
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;	
	}

	public List<Photo> getPhotoList() {
		return photoList;
	}

	public void setPhotoList(List<Photo> photoList) {
		this.photoList = photoList;
	}

	public List<Album> getAlbumList() {
		return albumList;
	}

	public void setAlbumList(List<Album> albumList) {
		this.albumList = albumList;
	}
	
	public void createAlbum() {
		String albumName = request.getParameter("albumName");
		System.out.println("Received request to create album " + albumName);
		Connection con = null;
		try {
			con = DBManager.getInstance().getConn();
			String sql = "INSERT INTO cs633.user_album (`USER_SEQ_ID`, `ALBUM_NAME`, `ALBUM_LOC`, `ALBUM_DESC`) VALUES(?,?,?,?)";
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, 2);
			pstmt.setString(2, albumName);
			pstmt.setString(3, albumName);
			pstmt.setString(4, "Album created from web interface.");
			int count = pstmt.executeUpdate();
			
		} catch (Exception e) {
			System.out.println("Error: " + e.getMessage());
		} finally {
			try {
				con.close();
			} catch (Exception e) { }
		}
	}
}
